<template>
    <div class="sidebar">
      <el-menu
        class="sidebar-el-menu"
        :default-active="onRoutes"
        :collapse="collapse"
        active-text-color="#20a0ff"
        unique-opened
        router
      >
            <template v-for="item in perssion">
                <template v-if="item.menusByPsetId">
                    <el-submenu :index="String(item.psetId)" :key="item.psetId">
                        <template slot="title">
                            <i :class="item.psetIcon"></i>

                            <span slot="title">{{ item.psetName }}</span>
                        </template>
                        <template v-for="subItem in item.menusByPsetId">
                            <el-submenu
                                v-if="subItem.menusByPsetId"
                                :index="subItem.mePath"
                                :key="subItem.meId"
                            >
                              <i :class="subItem.meIcon"></i>
                                <template slot="title">{{ subItem.meName }}</template>
                                <el-menu-item
                                    v-for="(threeItem,i) in subItem.meParentId"
                                    :key="i"
                                    :index="threeItem.mePath"
                                >{{ threeItem.meName }}</el-menu-item>
                            </el-submenu>
                            <el-menu-item
                                v-else
                                :index="subItem.mePath"
                                :key="subItem.meId"
                            >{{ subItem.meName }}
                            </el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="String(item.psetId)" :key="item.psetId">
                        <i class="el-icon-menu"></i>
                        <span slot="title">{{ item.psetName }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from './bus';
export default {
  name:'Sidebar',
    data() {
        return {
            collapse: false,
            items: [
                {
                    icon: 'el-icon-menu',
                    index: '1',
                    title: '客户模块',
                  subs:[
                    {
                      index:'customer',
                      title:'客户信息'
                    },
                    {
                      index:'culuechi',
                      title:'线索池'
                    },{
                      index:'cumergerecord',
                      title:'客户合并记录'
                    },{
                      index:'customermerge',
                      title:'客户合并'
                    },{
                      index:'check',
                      title:'审批'
                    },{
                      index:'dispose',
                      title:'集客'
                    },{
                      index:'log',
                      title:'日志'
                    }
                  ]
                },
                {
                    icon: 'el-icon-menu',
                    index: '2',
                    title: '售前模块',
                    subs:[
                      {
                        index:'business',
                        title:'售前商机管理'
                      },{
                        index:'salesfunnel',
                        title:'售前视图'
                      }
                    ]
                },
                {
                    icon: 'el-icon-menu',
                    index: '3',
                    title: '售中模块',
                  subs:[
                    {
                      index:'contractpage',
                      title:'合同'
                    },{
                      index:'contrantview',
                      title:'合同视图'
                    },{
                      index:'demopages',
                      title:'demopage_2'
                    },{
                      index:'ordepage',
                      title:'订单'
                    },{
                      index:'demopage',
                      title:'订单详情'
                    },{
                      index:'csdemo',
                      title:'新增订单'
                    },{
                      index:'datevie',
                      title:'漏斗'
                    }
                  ]
                },
                {
                    icon: 'el-icon-menu',
                    index: '4',
                    title: '售后模块',
                    subs: [
                        {
                            index: 'tain',
                            title: '维修工单'
                        },{
                            index: 'order',
                            title: '新增工单'
                        },{
                        index: 'purh',
                        title: '重复购买'
                      },{
                        index: 'serv',
                        title: '客服主页'
                      },{
                        index: 'arse',
                        title: 'QA武器库'
                      },{
                        index: 'plan',
                        title: '投诉管理'
                      }
                    ]
                },
                {
                    icon: 'el-icon-menu',
                    index: '5',
                    title:'产品模块',
                    subs:[
                      {
                        index:'product',
                        title:'产品'
                      },{
                        index:'addProduct',
                        title:'新增产品'
                      }
                    ]
                },
                {
                    icon: 'el-icon-menu',
                    index: '6',
                    title: '员工模块',
                    subs:[
                      {
                        index:'permissions',
                        title:'员工信息'
                      },
                      {
                        index:'deptManagement',
                        title:'部门'
                      }
                    ]
                }
            ],
          perssion:[],//权限
          perssionButton:[],//按钮
        };
    },
    computed: {
        onRoutes() {
            return this.$route.path.replace('/', '');
        }
    },
    created() {

        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', msg => {
            this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });
      this.init();
    },
    methods:{
        init(){
          let id = JSON.parse(sessionStorage.getItem('user')).employeesId;
          this.$axios.get("http://localhost:8088/user/meun",
            {params:{
                empId:id
              }}).then(v=>{
            this.perssion = v.data;
          }).catch();
          this.$axios.get("http://localhost:8088/user/perssion_button",
            {params:{
                empId:id
              }}).then(v=>{
                  v.data.forEach(item=>{
                      this.perssionButton.push(item.identifier);
                    })
                   sessionStorage.setItem("button",JSON.stringify(this.perssionButton));//把按钮标识符存起来*/
          }).catch();
        /*  this.perssion=[];//权限
          this.perssionButton=[];//按钮
          this.perssion=JSON.parse(sessionStorage.getItem('permissionssetList'));
          JSON.parse(sessionStorage.getItem('permissionButton')).forEach(item=>{
            this.perssionButton.push(item.identifier);
          })
          sessionStorage.setItem("button",JSON.stringify(this.perssionButton));//把按钮标识符存起来*/
        },

  }
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 180px;
}
.sidebar > ul {
    height: 100%;
}
</style>
